package com.club.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.club.common.R;
import com.club.entity.Activity;
import com.club.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动管理控制器
 */
@RestController
@RequestMapping("/activity")
@Tag(name = "活动管理", description = "活动相关接口")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 创建活动
     */
    @SaCheckLogin
    @PostMapping("/create")
    @Operation(summary = "创建活动", description = "创建新的活动")
    public R createActivity(@RequestBody Activity activity) {
        boolean success = activityService.createActivity(activity);
        return success ? R.ok().message("创建成功") : R.error().message("创建失败");
    }

    /**
     * 更新活动信息
     */
    @SaCheckLogin
    @PutMapping("/update")
    @Operation(summary = "更新活动", description = "更新活动信息")
    public R updateActivity(@RequestBody Activity activity) {
        boolean success = activityService.updateActivity(activity);
        return success ? R.ok().message("更新成功") : R.error().message("更新失败");
    }

    /**
     * 更新活动状态
     */
    @SaCheckLogin
    @PutMapping("/update-status/{id}/{status}")
    @Operation(summary = "更新活动状态", description = "更新活动的状态")
    public R updateActivityStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        boolean success = activityService.updateActivityStatus(id, status);
        return success ? R.ok().message("状态更新成功") : R.error().message("状态更新失败");
    }

    /**
     * 删除活动
     */
    @SaCheckLogin
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除活动", description = "删除指定的活动")
    public R deleteActivity(@PathVariable("id") Long id) {
        boolean success = activityService.deleteActivity(id);
        return success ? R.ok().message("删除成功") : R.error().message("删除失败");
    }

    /**
     * 根据ID查询活动
     */
    @SaCheckLogin
    @GetMapping("/get/{id}")
    @Operation(summary = "获取活动详情", description = "根据ID查询活动详情")
    public R getActivityById(@PathVariable("id") Long id) {
        Activity activity = activityService.getActivityById(id);
        return activity != null ? R.ok().data(activity) : R.error().message("活动不存在");
    }

    /**
     * 根据社团ID查询活动列表
     */
    @SaCheckLogin
    @GetMapping("/list-by-club/{clubId}")
    @Operation(summary = "获取社团活动列表", description = "根据社团ID查询活动列表")
    public R getActivitiesByClubId(@PathVariable("clubId") Long clubId) {
        List<Activity> activities = activityService.getActivitiesByClubId(clubId);
        return R.ok().data(activities);
    }

    /**
     * 查询所有活动
     */
    @SaCheckLogin
    @GetMapping("/list")
    @Operation(summary = "获取活动列表", description = "查询所有活动")
    public R getAllActivities(@RequestParam(value = "status", required = false) Integer status) {
        List<Activity> activities = activityService.getAllActivities(status);
        return R.ok().data(activities);
    }

    /**
     * 审批活动
     */
    @SaCheckLogin
    @PutMapping("/approve/{id}")
    @Operation(summary = "审批活动", description = "审批活动申请")
    public R approveActivity(@PathVariable("id") Long id,
                           @RequestParam("approvalStatus") Integer approvalStatus,
                           @RequestParam("approverId") Long approverId,
                           @RequestParam(value = "reason", required = false) String reason) {
        boolean success = activityService.approveActivity(id, approvalStatus, approverId, reason);
        return success ? R.ok().message("审批成功") : R.error().message("审批失败");
    }
}